﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySQL.Db;
using MySql.Data.MySqlClient;

namespace Gollie.Ajax
{
	public class InsertPendingPayment: AjaxResult
	{
		public string CustomerCode;

		public InsertPendingPayment() : base(null) { }

		public InsertPendingPayment(AjaxArg arg)
			: base(arg)
		{
		}

		protected override void Init(AjaxArg ajaxArg)
		{
			InsertPendingPaymentArg arg = (InsertPendingPaymentArg)ajaxArg;

			uint uid = Convert.ToUInt32(arg.tokenUser);
			
			try
			{
				decimal amount = Helper.GetDecimal(arg.samount) / 100;

				CustomerCode = Database.Queries.InsertPendingPayment(uid, amount);
			}
			catch (MySqlException mySqlEx)
			{
				this.ErrorCode = mySqlEx.Number;
				this.ErrorMessage = MySqlErrorCodes.GetInstance().GetMessage(mySqlEx);
				logger.Warn("InsertPendingPayment failed for userid {0}: {1}", arg.tokenUser, this.ErrorMessage);
			}
			catch (Exception e)
			{
				if (this.ErrorCode == 0)
					this.ErrorCode = 99;

				this.ErrorMessage = e.Message;
				logger.Warn("InsertPendingPayment failed for userid {0}: {1}", arg.tokenUser, this.ErrorMessage);
			}
		}
	}
}